import { trunkLineList } from "@/services/city-road";
import dayjs, { Dayjs } from "dayjs";
import { defineStore } from "pinia";
import { ref, shallowRef } from "vue";

const now = dayjs();

export const useCityRoadStore = defineStore("city-road", () => {
    const currentRoad = ref<null | any>(null);
    const weekRange = ref<[Dayjs, Dayjs]>([
        now.clone().subtract(2, "w"),
        now.clone().subtract(1, "w"),
    ]);
    const startTime = ref("09:00");
    const endTime = ref("17:00");
    const dateType = ref<number>(1);
    const indexType = ref<number>(2);

    const roads = shallowRef([]);
    const checkedRoads = shallowRef([]);

    async function fetchRoads(params: any) {
        const data = (await trunkLineList(params)) as any;
        roads.value = data || [];
        return data;
    }

    function updateRoad(road: any | null) {
        currentRoad.value = road;
    }
    function updateCheckedRoads(roads: any, _: any) {
        currentRoad.value = roads;
    }
    function updateWeekRange(
        range: [Dayjs, Dayjs] | [string, string],
        _: [string, string]
    ) {
        weekRange.value = range as [Dayjs, Dayjs];
        updateRoad(null);
    }
    function updateStartTime(type: any) {
        startTime.value = type;
        updateRoad(null);
    }
    function updateEndTime(type: any) {
        endTime.value = type;
        console.log(type);

        updateRoad(null);
    }
    function updateDateType(type: any) {
        dateType.value = type;
        updateRoad(null);
    }
    function updateIndexType(type: any) {
        indexType.value = type;
        updateRoad(null);
    }

    return {
        currentRoad,
        updateRoad,

        weekRange,
        updateWeekRange,

        startTime,
        updateStartTime,

        endTime,
        updateEndTime,

        dateType,
        updateDateType,

        indexType,
        updateIndexType,

        roads,
        fetchRoads,

        checkedRoads,
        updateCheckedRoads,
    };
});
